package com.frkj.order.controller;

import com.frkj.order.cache.BusinessCache;
import com.frkj.order.model.Operator;
import com.frkj.order.util.ShowMessage;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class OperatorController extends Controller {

    public void index() {
        // 管理员列表
        List<Operator> operatorList = Operator.me.find("select * from operator order by id desc");
        setAttr("operatorList", operatorList);

        // 管理员权限列表
        List<String> operatorBusinessList = new ArrayList<String>();
        for(Operator operator : operatorList) {
            List<Record> recordList = Db.find("select name from business where id in (select business_id from operator_business where operator_id=?)", operator.getInt("id"));
            String[] businessNames = new String[recordList.size()];
            for(int i = 0; i < recordList.size(); i++) {
                businessNames[i] = recordList.get(i).getStr("name");
            }
            operatorBusinessList.add(Arrays.toString(businessNames));
        }
        setAttr("operatorBusinessList", operatorBusinessList);

        render("index.html");
    }

    public void add() {
        setAttr("businessList", BusinessCache.getBusinessList());
        render("add.html");
    }

    public void save() {
        // 保存管理员
        Operator operator = new Operator();
        operator.set("name", getPara("name"));
        String password = getPara("password");
        String enPassword = DigestUtils.sha1Hex(password);
        operator.set("password", enPassword);
        operator.set("create_time", new Date());
        operator.save();

        // 保存权限
        Integer[] businessIds = getParaValuesToInt("businessId");
        for(Integer bid : businessIds) {
            Record record = new Record();
            record.set("operator_id", operator.get("id"));
            record.set("business_id", bid);
            Db.save("operator_business", record);
        }

        setAttr("showMessage", ShowMessage.ADD_SUCCESS);
        index();
    }

    public void edit() {
        int operatorId = getParaToInt("id");
        setAttr("operator", Operator.me.findById(operatorId));
        setAttr("businessList", BusinessCache.getBusinessList());

        List<Record> recordList = Db.find("select business_id from operator_business where operator_id=?", operatorId);
        List<Integer> operatorBusinessList = new ArrayList<Integer>(recordList.size());
        for(Record record : recordList) {
            operatorBusinessList.add(record.getInt("business_id"));
        }
        setAttr("operatorBusinessList", operatorBusinessList);

        String type = getPara("type");
        render("edit" + type  + ".html");
    }

    public void updatePassword() {
        Operator operator = getModel(Operator.class);
        String password = getPara("password");
        String enPassword = DigestUtils.sha1Hex(password);
        operator.set("password", enPassword);
        operator.update();

        setAttr("showMessage", ShowMessage.UPDATE_SUCCESS);
        index();
    }

    public void updateBusiness() {
        Operator operator = getModel(Operator.class);
        Integer[] businessIds = getParaValuesToInt("businessId");

        Db.update("delete from operator_business where operator_id=?", operator.getInt("id"));
        for(Integer bid : businessIds) {
            Record record = new Record();
            record.set("operator_id", operator.getInt("id"));
            record.set("business_id", bid);
            Db.save("operator_business", record);
        }

        setAttr("showMessage", ShowMessage.UPDATE_SUCCESS);
        index();
    }

    public void delete() {
        Operator.me.deleteById(getParaToInt("id"));

        setAttr("showMessage", ShowMessage.DELETE_SUCCESS);
        index();
    }
}
